import { Form } from '@alifd/next';
import React, { useEffect, useState } from 'react';
import { FieldsGrid, TitleGrid } from '../../stores/definitions_order';
import { t } from '@lingui/macro';
import { useStore } from '@/hooks';
import { observer } from 'mobx-react-lite';
import TicketTable from './TicketTable';

const OrderInfo = (props) => {
  const { orderId, task } = props;
  const [order, setOrder] = useState({});
  const { rsTaskStore: store } = useStore();

  useEffect(() => {
    if (orderId) {
      (async () => {
        const res = await store.getOrderInfo(orderId);
        const data = _.cloneDeep(res);
        _.assign(data, { taskInfo: task });
        setOrder(data);
      })();
    }
  }, []);

  return (
    <Form
      inline
      isPreview={Boolean(true)}
      value={order}
    >
      <TitleGrid title={t`基本信息`} />
      <FieldsGrid
        formData={order}
        mode="view"
        fields={[
          'orderNumber', 'crDate', 'rsOrder.orderStatus', 'rsOrder.payType', 'price', 'refundInfos[0].cancelTime', 'companyName',
        ]}
      />
      <TitleGrid title={t`客户资料`} />
      <FieldsGrid
        formData={order}
        mode="view"
        fields={[
          'rsOrder.passengerName', 'rsOrder.passengerPhone',
        ]}
      />
      <TitleGrid title={t`预定信息`} />
      <FieldsGrid
        formData={order}
        mode="view"
        fields={[
          'rsOrder.startMainAreaText',
          'rsOrder.endMainAreaText',
          'rsOrder.startStationName',
          'rsOrder.endStationName',
          'rsOrder.beginName',
          'rsOrder.endName',
          'rsOrder.predictBeginDate']}
      />
      <TitleGrid title={t`车票信息`} />
      <TicketTable
        dataSource={order?.rsOrder?.ticketInfos}
        refundInfos={order?.refundInfos || []}
      />
      <TitleGrid title={t`备注`} />
      <FieldsGrid
        formData={order}
        mode="view"
        fields={[
          'rsOrder.remarks', 'refundInfos[0].cancelRemark',
        ]}
      />
    </Form>
  );
};

export default observer(OrderInfo);
